﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using jade.core;
using jade.wrapper;
using jade.wrapper.gateway;
using System.Data;
using Newtonsoft.Json;

namespace CMS1
{
    public partial class _Default : System.Web.UI.Page
    {
        /*protected override void OnInit(EventArgs e) 
         { 
         }*/
        DatabaseWrapper db;
        public void Application_Start(object sender, EventArgs e)
        {
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            /*using (CMS1LinqDataContext Data = new CMS1LinqDataContext())
            {
                Task task = Data.Tasks.First();
            }
            using (DataClasses1DataContext data = new DataClasses1DataContext())
            {
                //Chair_task cTask = data.Chair_tasks.First();
                Chair chair = data.Chairs.First();
                TextBox1.Text = chair.name;
            }*/
            if (!IsPostBack)
            {
                Runtime rt = Runtime.instance();
                Profile p = new ProfileImpl(false);
                jade.wrapper.AgentContainer ac = rt.createMainContainer(p);
                AgentController dummy = ac.createNewAgent("Dummy", "CMS1.DummyReceiver", new Object[0]);
                AgentController scheduler = ac.createNewAgent("Scheduler", "CMS1.Scheduler", new Object[0]);
                dummy.start();
                scheduler.start();
                JadeGateway.init("CMS1.GatewayAgent", null);
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            sendToGatewayAgent();
        }
        protected void sendToGatewayAgent()
        {
            Medium m = new Medium();
            m.RecipientAgent = "Dummy";
            Message msg = new Message();
            msg.Purpose = 1;
            msg.Data = "Test";
            m.CarriedData = JsonConvert.SerializeObject(msg);
            try
            {
                JadeGateway.execute(m);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.InnerException);
                Console.WriteLine(e.Message);
                throw new Exception(e.Message);
            }
            //HttpContext.Current.Response.Redirect("http://www.google.com/");
            TextBox1.Text = m.CarriedData;
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            db = new DatabaseWrapper();
            db.initConnection(DatabaseWrapper.connString);
            
            object[] tasks = (object[])db.selectColumn("name").from("Task").execute();
            foreach (string task in tasks) {
                ListBox1.Items.Add(task.ToString());
            }

            /*
            object[] row = (object[])db.selectRowFrom("Chair").where("id='1'").execute();
            string[] strRow = row.Select(o => o.ToString()).ToList().ToArray();
            foreach (string name in strRow) {
                ListBox1.Items.Add(name.ToString());
            }*/

            /*
                db.insert_into("Phase").columns("id, start_date, length, conf_id").values("'5','" + DateTime.Now.Date + "','20','1'").execute();
                db.delete_from("Phase").where("id='5'").or("id='4'").execute();
            */

            /*
             object[][] rows = (object[][])db.select("name, conf_id").from("Task").execute();
            foreach (object[] row in rows) {
                string[] strRow = row.Select(o => o.ToString()).ToList().ToArray();
                ListBox1.Items.Add(strRow[0]);
                ListBox2.Items.Add(strRow[1]);
            }*/

            /*object[][] x = (object[][])db.selectAll().from("Chair").where("id='5'").execute();
            if (x == null)
            {
                Response.Write("NULL");
            }
            else
            {
                Response.Write(x.Length + "   ");
            }*/
        }
        public void display(string s)
        {
            TextBox1.Text = s;
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            try{}
            catch (Exception ex){
                System.Diagnostics.Debug.WriteLine(ex.Message + "  " + ex.InnerException);
            }
            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
            message.To.Add("mohamed-elhadi@live.com");
            message.Subject = "Blah " + DateTime.Now.ToShortTimeString();
            message.From = new System.Net.Mail.MailAddress("mas-cm@live.com");
            message.Body = "This is the message body";
            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.live.com");
            smtp.EnableSsl = true;
            smtp.Credentials = new System.Net.NetworkCredential("mas-cm@live.com", "it-can-be");
            smtp.Send(message);
        }
    }
}
//DataRow[] rows = dt.Select();
//DataRow[] rows = dt.Rows.Cast<DataRow>().ToArray()
//object[] row;
//string[] strRow = row.Cast<string>().ToArray();
//string[] strRow = string.Join(",", row).Split(',');

/*
class MyObject{
    public string Foo { get; set; }
    public int Foo { get; set; }
}
using System.Data.DataSetExtensions;
List<MyObject> list = (from row in table.AsEnumerable()
                       select new MyObject
                       {
                            Foo = row.Field<string>("foo"),
                            Bar = row.Field<int>("bar")
                       }).ToList();
 
 
public Customer ConvertRowToCustomer(DataRow row){
   Customer result = new Customer();
   result.ID = row.Field<int>("ID");
   result.Name = row.Field<string>("CustomerName");
   return result;
}
 */